<?php

// +----------------------------------------------------------------------
// | Category 模型
// +----------------------------------------------------------------------
// | @author fancsen
// +----------------------------------------------------------------------

namespace app\api\model;

use app\api\model\BaseModel;

class Category extends BaseModel 
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'category';
    //自动时间戳
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    //protected $createTime = 'create_at';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';
    //隐藏字段
    protected $hidden = ['delete_time','update_time','topic_img_id'];
    //关联image
    public function topicImg()
	{
		return $this->belongsTo('Image','topic_img_id','id');
    }	
    public function products()
    {
        return $this->hasMany('Product','category_id','id');
    }

    /**
     * 获取指定类目下的商品
     */
    public static function getProductsByCategoryId($id)
    {
        $result = self::with(['topicImg','products'])->find($id);

        return $result;
    }
}